package com.hopu.emp_demo.controller;

import com.hopu.emp_demo.entity.Employee;
import com.hopu.emp_demo.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
@RestController
public class EmployeeController {

    /**
     * GET all --->  /employees
     * GET id ---> /employee/1
     * POST ---> /employee
     * PUT ---> /employee
     * DELETE ---> /employee/1
     */

    @Autowired
    private EmployeeService employeeService;

    @ApiOperation("查询所有员工信息")
    @GetMapping("/employees")
    public ResponseEntity<List<Employee>> findAll(){
        List<Employee> all = employeeService.findAll();
        return ResponseEntity.ok(all);
    }

    @ApiOperation("通过id查询员工")
    @GetMapping("/employee/{id}")
    public ResponseEntity<Employee> findById(@PathVariable("id")Long id){
        Employee emp = employeeService.findById(id);
        return ResponseEntity.ok(emp);
    }

    //id=1&name=xx&age=10   {id:1,name:xx,age:10}
    @ApiOperation("添加员工")
    @PostMapping("/employee")
    public ResponseEntity<String> addEmp(@RequestBody Employee employee){
        employeeService.add(employee);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("更新员工")
    @PutMapping("/employee")
    public ResponseEntity<String> modifyEmp(@RequestBody Employee employee){
        employeeService.modify(employee);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("删除员工")
    @DeleteMapping("/employee/{id}")
    public ResponseEntity<String> removeEmp(@PathVariable("id")Long id){
        employeeService.removeById(id);
        return ResponseEntity.ok("ok");
    }
}
